package com.ygctpap.until;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class RabbitmqProduUntil {

   public static Map<String, Channel> m_map_exchange_channel = new HashMap<>();
   public static Map<String, List<String>> m_map_exchange_queue = new HashMap<>();

    public static boolean initExchange(String exchangeName) throws IOException, TimeoutException {
        if (m_map_exchange_channel.get(exchangeName) == null) {
            ConnectionFactory factory = new ConnectionFactory();
            //设置RabbitMQ相关信息
            factory.setHost("localhost");
            //创建一个新的连接
            Connection connection = factory.newConnection();
            //创建一个通道
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(exchangeName, "topic");
            m_map_exchange_channel.put(exchangeName, channel);
        }
        return true;
    }

    public static boolean initQueue(String exchangeName, String queue_name) throws IOException, TimeoutException {
        // 1. 初始化交换机，确保交换机存在
        if (initExchange(exchangeName) == true) {
            // 2. 交换机中没有队列，创建队列

            if(m_map_exchange_queue.get(exchangeName) == null){
                List<String> lst = new ArrayList<>();
                m_map_exchange_queue.put(exchangeName,lst);
            }
            if (m_map_exchange_queue.get(exchangeName).contains(queue_name) == false) {

                m_map_exchange_channel.get(exchangeName).queueDeclare(queue_name, true, false, false, null);
                List<String> lst = m_map_exchange_queue.get(exchangeName);
                lst.add(queue_name);
                m_map_exchange_queue.put(exchangeName, lst);
                return true;

            }
            // 3. 交换机中有对列，直接返回为真
            if (m_map_exchange_queue.get(exchangeName) != null && m_map_exchange_queue.get(exchangeName).contains(queue_name) == true) {
                return true;
            }
        }
        return false;
    }

    public static boolean initRoutingKey(String exchangeName, String queue_name, String routing_key) throws IOException, TimeoutException {
        if (initExchange(exchangeName) == true && initQueue(exchangeName, queue_name) == true) {
            m_map_exchange_channel.get(exchangeName).queueBind(queue_name, exchangeName, routing_key);
            return true;
        }
        return false;
    }

    public static boolean productMsg(String exchange_name, String queue_name, String routing_key, Object obj) throws IOException, TimeoutException {
        if (initRoutingKey(exchange_name, queue_name, routing_key) == true) {
            String message = obj.toString();
            m_map_exchange_channel.get(exchange_name).basicPublish(exchange_name, routing_key, null, obj.toString()
                    .getBytes("UTF-8"));
            return true;
        }
        return false;
    }

//    public void rabbitmqCustomer(String routingkey) throws IOException, TimeoutException {
//        // 创建连接工厂
//        ConnectionFactory factory = new ConnectionFactory();
//        //设置RabbitMQ地址
//        factory.setHost("localhost");
//        //创建一个新的连接
//        Connection connection = factory.newConnection();
//        //创建一个通道
//        Channel channel = connection.createChannel();
//        //交换机声明（参数为：交换机名称；交换机类型）
//        channel.exchangeDeclare(exchange_name, "topic");
//        //获取一个临时队列
//
//        //接受 USA 信息
//        channel.queueDeclare(queue_name, true, false, false, null);
//        channel.queueBind(queue_name, exchange_name, routingkey);
//
//        Consumer consumer = new DefaultConsumer(channel) {
//            @Override
//            public void handleDelivery(String consumerTag, Envelope envelope,
//                                       AMQP.BasicProperties properties, byte[] body)
//                    throws IOException {
//                String message = new String(body, "UTF-8");
//                System.out.println(" 接收到消息 '" + message + "'");
//            }
//        };
//
//        channel.basicConsume(queue_name, true, consumer);
//
//
//    }


}
